package com.mashibing.mall.ware.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.mashibing.common.exception.ServiceException;
import com.mashibing.common.utils.R;
import com.mashibing.mall.ware.feign.ProductFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashibing.common.utils.PageUtils;
import com.mashibing.common.utils.Query;

import com.mashibing.mall.ware.dao.WareSkuDao;
import com.mashibing.mall.ware.entity.WareSkuEntity;
import com.mashibing.mall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * 商品库存 Service 实现类
 */
@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Resource
    private ProductFeignService productFeignService;

    @Resource
    private WareSkuDao wareSkuDaoSkuDao;
    @Autowired
    private WareSkuDao wareSkuDao;

    /**
     * 商品库存 - 分页
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<WareSkuEntity> queryWrapper = this.buildQueryWrapper(params);
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                new QueryWrapper<WareSkuEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 商品库存 - 增加库存
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {

        // 根据 sku id、仓库 id 查询商品库存列表
        List<WareSkuEntity> wareSkuEntityList = this.getBaseMapper().selectList(new QueryWrapper<WareSkuEntity>()
               .eq("sku_id", skuId).eq("ware_id", wareId));

        // 如果商品库存列表为空，则新增库存
        if(CollectionUtil.isEmpty(wareSkuEntityList)){

            // 构建待新增的商品库存
            WareSkuEntity wareSkuEntity = new WareSkuEntity();

            // 设置 sku id、仓库 id、库存数量
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);

            // 设置锁定库存为 0
            wareSkuEntity.setStockLocked(0);

            try{
                // 根据 sku id 查询商品信息
                R info = productFeignService.info(skuId);
                Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");

                // 如果响应码为 0，设置 sku 名称
                if (info.getCode() == 0) {
                    String skuName = (String) data.get("skuName");
                    wareSkuEntity.setSkuName(skuName);
                }
            } catch (Exception e) {
                throw new ServiceException("新增库存失败");
            }

            // 新增商品库存
            wareSkuDaoSkuDao.insert(wareSkuEntity);
        } else {

            // 如果商品库存列表不为空，则更新库存数量
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }
    }

    /**
     * 构造查询条件
     */
    private QueryWrapper<WareSkuEntity> buildQueryWrapper(Map<String, Object> params) {

        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();

        // 根据 sku id 查询
        String skuId = (String) params.get("skuId");
        if(!StringUtils.isEmpty(skuId)){
            queryWrapper.eq("sku_id",skuId);
        }

        // 根据仓库 id 查询
        String wareId = (String) params.get("wareId");
        if(!StringUtils.isEmpty(wareId)){
            queryWrapper.eq("ware_id",wareId);
        }

        return queryWrapper;
    }

}
